// webpack相关的配置文件
'use strict'// 开启严格模式
// 这是commen js 遵守noodjs规范的导包的方式
// -导入require()
// -导出 module.export || exports 内置的path路径模块
// path主要是操作路径的模块
const path = require('path')
// 导入自定义的包 导入项目的配置的文件 setting.js文件中的配置
const defaultSettings = require('./src/settings.js')
// 将传入的相对路径变成绝对路径 dir接受的是相对的路径
function resolve(dir) {
  // __dirname拿到的绝对的路径(获取当前的文件) 路径进行拼接
  return path.join(__dirname, dir)
}
// 这是设置网页的标题 ,name可以保存网页的标题 导入是上面的defaultSettings 有就用对象里面的值,没有就用后面的
const name = defaultSettings.title || 'vue Admin Template' // page title

// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following methods:
// port = 9528 npm run dev OR npm run dev --port = 9528
// 设置项目文件的端口号,如果没有配置的话就选用最后面的端口号
const port = process.env.port || process.env.npm_config_port || 9528 // dev port

// All configuration item explanations can be find in https://cli.vuejs.org/config/
// 脚手架vue-cli相关的配置项
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  // 默认的话只能在服务器上运行,/是本地运行, 配置生成的资源index.html是否可以在什么环境下打开运行
  // 默认/ 只能在服务器的环境下运行,如果你想要任何环境下运行,我们可以使用将他的值设置为"./'
  // 值默认是/ 需要自己进行修改
  publicPath: './',
  outputDir: 'dist', // 打包后导出资源的目录的名称
  assetsDir: 'static', // 打包之后资源的目录的中存储的静态资源的文件的目录 js css
  // 是否开启exlint的检查 开发中打开 线上的时候不打开
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false, // 是否开启map映射
  // 脚手架提供的服务器的相关的配置
  devServer: {
    port: port, // 设置端口号
    open: true, // 设置浏览器自动打开
    // 项目当中如果产生 一些错误 浏览器中的错误的提示方式 设置代码的错误的登记是否在浏览器中显示
    overlay: {
      warnings: false, // 提示
      errors: true// 跑错
    }
    // 加载mock服务
    // before: require('./mock/mock-server.js')
  },
  // webpack相关的配置,后面是js对象 这个是对象的配置
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        // 能够使用@的原因 src 绝对路径
        '@': resolve('src')
      }
    }
  },
  // 这是webpack他相关的配置 函数配置 上面是对象的配置
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])

    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    config
      .when(process.env.NODE_ENV !== 'development',
        config => {
          config
            .plugin('ScriptExtHtmlWebpackPlugin')
            .after('html')
            .use('script-ext-html-webpack-plugin', [{
            // `runtime` must same as runtimeChunk name. default is `runtime`
              inline: /runtime\..*\.js$/
            }])
            .end()
          config
            .optimization.splitChunks({
              chunks: 'all',
              cacheGroups: {
                libs: {
                  name: 'chunk-libs',
                  test: /[\\/]node_modules[\\/]/,
                  priority: 10,
                  chunks: 'initial' // only package third parties that are initially dependent
                },
                elementUI: {
                  name: 'chunk-elementUI', // split elementUI into a single package
                  priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
                  test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
                },
                commons: {
                  name: 'chunk-commons',
                  test: resolve('src/components'), // can customize your rules
                  minChunks: 3, //  minimum common number
                  priority: 5,
                  reuseExistingChunk: true
                }
              }
            })
          // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
          config.optimization.runtimeChunk('single')
        }
      )
  }
}
